package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.service.mongo.CommentService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public Integer saveMovementComment(Comment comment) {
        //1. 处理comment对象(需要前端传入publishId  commentType userId  conetent)
        comment.setCreated(System.currentTimeMillis());
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        comment.setPublishUserId(movement.getUserId());//设置被操作对象所属人

        //2. 向评论表中保存数据
        mongoTemplate.save(comment);

        //3. 向动态表中做修改操作
        if (comment.getCommentType() == 1) {
            movement.setLikeCount(movement.getLikeCount() + 1);
        } else if (comment.getCommentType() == 2) {
            movement.setCommentCount(movement.getCommentCount() + 1);
        } else if (comment.getCommentType() == 3) {
            movement.setLoveCount(movement.getLoveCount() + 1);
        }
        mongoTemplate.save(movement);

        //4. 返回操作数量
        if (comment.getCommentType() == 1) {
            return movement.getLikeCount();
        } else if (comment.getCommentType() == 2) {
            return movement.getCommentCount();
        } else if (comment.getCommentType() == 3) {
            return movement.getLoveCount();
        } else {
            return 0;
        }
    }

    @Override
    public Integer deleteMovementComment(Comment comment) {
        //1. 处理comment对象(需要前端传入publishId  commentType userId)
        Query query = new Query(
                Criteria.where("commentType").is(comment.getCommentType())
                        .and("publishId").is(comment.getPublishId())
                        .and("userId").is(comment.getUserId())
        );
        //2. 向评论表中删除数据
        mongoTemplate.remove(query, Comment.class);

        //3. 向动态表中做修改操作
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        if (comment.getCommentType() == 1) {
            movement.setLikeCount(movement.getLikeCount() - 1);
        } else if (comment.getCommentType() == 2) {
            movement.setCommentCount(movement.getCommentCount() - 1);
        } else if (comment.getCommentType() == 3) {
            movement.setLoveCount(movement.getLoveCount() - 1);
        }
        mongoTemplate.save(movement);

        //4. 返回操作数量
        if (comment.getCommentType() == 1) {
            return movement.getLikeCount();
        } else if (comment.getCommentType() == 2) {
            return movement.getCommentCount();
        } else if (comment.getCommentType() == 3) {
            return movement.getLoveCount();
        } else {
            return 0;
        }
    }

    @Override
    public PageBeanVo findMovementCommentList(String movementId, Integer pageNum, Integer pageSize) {
        //1. 确定查询条件
        Query query = new Query(
                Criteria.where("publishId").is(new ObjectId(movementId))
                        .and("commentType").is(2)
        ).with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);

        //2. 执行查询
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        //3. 统计数量
        long count = mongoTemplate.count(query, Comment.class);

        //4. 返回pageBeanVo
        return new PageBeanVo(pageNum, pageSize, count, commentList);
    }

    @Override
    public PageBeanVo countCommentInfo(Long userId, Integer commentType, Integer pageNum, Integer pageSize) {
        //1. 根据动态发布人id和操作类型查询
        Query query = new Query(
                Criteria.where("publishUserId").is(userId)
                        .and("commentType").is(commentType)
        ).with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);

        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        //2. 统计
        long count = mongoTemplate.count(query, Comment.class);

        //3. 返回
        return new PageBeanVo(pageNum,pageSize,count,commentList);
    }

    //视频点赞

    public void saveVideoLikeComment(Comment comment) {

        //获取视频拥有着id
        Long userId = mongoTemplate.findById(comment.getPublishId(), Video.class).getUserId();
        comment.setCreated(System.currentTimeMillis());
        comment.setPublishUserId(userId);
        mongoTemplate.save(comment);
    }

    //视频取消点赞

    public void deleteVideoLike(Comment comment) {
        Query query = new Query(Criteria.where("userId").is(comment.getUserId()).and("publishId").is(comment.getPublishId()).and("commentType").is(comment.getCommentType()));
        mongoTemplate.remove(query,Comment.class);
    }

    //视频评论列表

    public PageBeanVo findVideoComments(String videoId, Integer pageNum, Integer pageSize) {
        Query query = new Query(
                Criteria.where("publishId").is(new ObjectId(videoId))
                        .and("commentType").is(5)
        ).with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        //统计总数
        long count = mongoTemplate.count(query, Comment.class);
        //返回pageBeanVo
        return new PageBeanVo(pageNum,pageSize,count,commentList);
    }

    //保存视频评论

    public void saveVideoComment(Comment comment) {
        comment.setCreated(System.currentTimeMillis());
        //获取video参数
        Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);
        comment.setPublishUserId(video.getUserId());
        //保存视频评论
        mongoTemplate.save(comment);
        //判断是点赞还是评论
        if (comment.getCommentType() == 4){
            video.setLikeCount(video.getLikeCount() + 1);
        }if (comment.getCommentType() == 5){
            video.setCommentCount(video.getCommentCount() + 1);
        }
        mongoTemplate.save(video);

    }

    //查询评论详情

    public Comment findMovementCommentLike(ObjectId commentId) {
        //根据主键查询动态详情
        return mongoTemplate.findById(commentId,Comment.class);
    }

    //修改更新

    public Integer updateComment(Comment comment) {
        mongoTemplate.save(comment);
        return comment.getLikeCount();
    }


}
